﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using RapidWebDev.SearchIndex.Contract;

namespace RapidWebDev.SearchIndex.DocumentFields
{
	/// <summary>
	/// Interface to access indexing document field.
	/// </summary>
	internal class DateTimeDocumentField : BaseDocumentField
	{
		/// <summary>
		/// Indexing document field type.
		/// </summary>
		public override IndexDocFieldValueTypes FieldType { get { return IndexDocFieldValueTypes.DateTime; } }

		/// <summary>
		/// Convert indexing document field message to lucene fields.
		/// </summary>
		/// <param name="indexDocField"></param>
		/// <returns></returns>
		public override Fieldable[] GetLuceneFields(IndexDocFieldMessage indexDocField)
		{
			if (indexDocField.Values == null || indexDocField.Values.Count == 0)
				return new Field[0];

			DateTime dateTime;
			if (DateTime.TryParse(indexDocField.Values[0], out dateTime))
			{
				List<Fieldable> fields = new List<Fieldable>
				{
					new Field(indexDocField.Name, DateTools.DateToString(dateTime, DateTools.Resolution.SECOND), Field.Store.YES, Field.Index.NOT_ANALYZED),
					new Field(LuceneFields.FieldType(indexDocField.Name), this.FieldType.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)
				};

				if (indexDocField.Sortable)
					fields.Add(new Field(LuceneFields.SortableFieldName(indexDocField.Name), DateTools.DateToString(dateTime, DateTools.Resolution.SECOND), Field.Store.YES, Field.Index.NOT_ANALYZED));

				return fields.ToArray();
			}

			return new Field[0];
		}

		/// <summary>
		/// Resolve original field value from Lucene stored field value.
		/// </summary>
		/// <param name="luceneStoredFieldValue"></param>
		/// <returns></returns>
		public override string ResolveOriginalFieldValue(string luceneStoredFieldValue)
		{
			return DateTools.StringToDate(luceneStoredFieldValue).ToString(CultureInfo.InvariantCulture);
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected override Query BuildBetweenQuery(SearchFilterMessage searchFilterMessage)
		{
			if (searchFilterMessage.Values == null || searchFilterMessage.Values.Count != 2) return null;

			DateTime dateTime;
			string lowerTerm = searchFilterMessage.Values[0];
			if (DateTime.TryParse(lowerTerm, out dateTime))
				lowerTerm = DateTools.DateToString(dateTime, DateTools.Resolution.SECOND);
			else
				lowerTerm = DateTools.DateToString(DateTime.MinValue, DateTools.Resolution.SECOND);

			string upperTerm = searchFilterMessage.Values[1];
			if (DateTime.TryParse(upperTerm, out dateTime))
				upperTerm = DateTools.DateToString(dateTime, DateTools.Resolution.SECOND);
			else
				upperTerm = DateTools.DateToString(DateTime.MaxValue, DateTools.Resolution.SECOND);

			return new TermRangeQuery(searchFilterMessage.FieldName, lowerTerm, upperTerm, true, true);
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected override Query BuildGreaterThanQuery(SearchFilterMessage searchFilterMessage)
		{
			if (searchFilterMessage.Values == null || searchFilterMessage.Values.Count != 1) return null;

			DateTime dateTime;
			string lowerTerm = searchFilterMessage.Values[0];
			if (DateTime.TryParse(lowerTerm, out dateTime))
				lowerTerm = DateTools.DateToString(dateTime, DateTools.Resolution.SECOND);
			else
				lowerTerm = DateTools.DateToString(DateTime.MinValue, DateTools.Resolution.SECOND);

			string upperTerm = DateTools.DateToString(DateTime.MaxValue, DateTools.Resolution.SECOND);
			return new TermRangeQuery(searchFilterMessage.FieldName, lowerTerm, upperTerm, false, true);
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected override Query BuildGreaterEqualThanQuery(SearchFilterMessage searchFilterMessage)
		{
			if (searchFilterMessage.Values == null || searchFilterMessage.Values.Count != 1) return null;

			DateTime dateTime;
			string lowerTerm = searchFilterMessage.Values[0];
			if (DateTime.TryParse(lowerTerm, out dateTime))
				lowerTerm = DateTools.DateToString(dateTime, DateTools.Resolution.SECOND);
			else
				lowerTerm = DateTools.DateToString(DateTime.MinValue, DateTools.Resolution.SECOND);

			string upperTerm = DateTools.DateToString(DateTime.MaxValue, DateTools.Resolution.SECOND);

			return new TermRangeQuery(searchFilterMessage.FieldName, lowerTerm, upperTerm, true, true);
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected override Query BuildLessThanQuery(SearchFilterMessage searchFilterMessage)
		{
			if (searchFilterMessage.Values == null || searchFilterMessage.Values.Count != 1) return null;

			string lowerTerm = DateTools.DateToString(DateTime.MinValue, DateTools.Resolution.SECOND);

			DateTime dateTime;
			string upperTerm = searchFilterMessage.Values[0];
			if (DateTime.TryParse(upperTerm, out dateTime))
				upperTerm = DateTools.DateToString(dateTime, DateTools.Resolution.SECOND);
			else
				upperTerm = DateTools.DateToString(DateTime.MaxValue, DateTools.Resolution.SECOND);

			return new TermRangeQuery(searchFilterMessage.FieldName, lowerTerm, upperTerm, true, false);
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected override Query BuildLessEqualThanQuery(SearchFilterMessage searchFilterMessage)
		{
			if (searchFilterMessage.Values == null || searchFilterMessage.Values.Count != 1) return null;

			string lowerTerm = DateTools.DateToString(DateTime.MinValue, DateTools.Resolution.SECOND);

			DateTime dateTime;
			string upperTerm = searchFilterMessage.Values[0];
			if (DateTime.TryParse(upperTerm, out dateTime))
				upperTerm = DateTools.DateToString(dateTime, DateTools.Resolution.SECOND);
			else
				upperTerm = DateTools.DateToString(DateTime.MaxValue, DateTools.Resolution.SECOND);

			return new TermRangeQuery(searchFilterMessage.FieldName, lowerTerm, upperTerm, true, true);
		}
	}
}
